package com.xtkj.superclub_activity.controller;

import com.xtkj.commons.JsonResult;
import com.xtkj.commons.SnowflakeIdWorker;
import com.xtkj.superclub_activity.service.IActivityService;
import com.xtkj.superclub_activity.service.IUserTeamService;
import com.xtkj.superclub_entity.entity.activity.Activity;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IUserTeamService userTeamService;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    //查询热门活动列表
    @GetMapping("/getHotActivities")
    public JsonResult getHotActivityList(){
        try {
            return JsonResult.success().setCode(200).setMsg("查询热门活动列表成功").setData(activityService.getHotActivityList());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询错误");
        }
    }

    //查询同城活动列表
    @GetMapping("/getCityActivityList")
    public JsonResult getCityActivityList(@Param("location") String location){
        try {
            return JsonResult.success().setCode(200).setMsg("查询同城活动列表成功")
                    .setData(activityService.getCityActivityList(location));
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询错误");
        }
    }

    //最新活动列表
    @GetMapping("/getNewsActiviyList")
    public JsonResult getNewsActiviyList(){
        try {
            return JsonResult.success().setCode(200).setMsg("查询最新活动列表成功")
                    .setData(activityService.getNewsActiviyList());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询错误");
        }
    }

    ////活动详情信息
    @GetMapping("/getActivtiyInfo/{activityId}")
    public JsonResult getActivtiyInfo(@PathVariable("activityId") String activityId){
        System.out.println("activityId is "+activityId);
        try {
            return JsonResult.success().setCode(200).setMsg("查询活动详情成功")
                    .setData(activityService.getActivtiyInfo(activityId));
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询错误");
        }
    }

    //发布活动
    @PostMapping("/sendActivity")
    public JsonResult sendActivity(@RequestBody Activity activity) {
        System.out.println("我进来了。。sendActivity");
        try {
            activity.setActivityId(snowflakeIdWorker.nextId()+"");
            activityService.sendActivity(activity);
            return JsonResult.success().setCode(200).setMsg("发布活动成功");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg(e.getMessage());
        }

    }

    //根据用户的id查询用户所属的俱乐部
    @GetMapping("/getTeamsByUserId")
    public JsonResult getTeamsByUserId(HttpServletRequest request){
        //String token = request.getHeader("token");
        //String userId = TokenUtil.getUserId(token);
        String userId = "262234808388681728";

        try {
            return JsonResult.success().setCode(200).setMsg("查询用户俱乐部成功").setData(userTeamService.getTeamsByUserId(userId));
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询用户俱乐部成功失败");
        }

    }

    //报名活动
    @PostMapping("/attendActivtiy/{activityId}")
    public JsonResult attendActivtiy(@PathVariable("activityId") String activityId, HttpServletRequest request) {
        //String token = request.getHeader("token");
        //String userId = TokenUtil.getUserId(token);
        String userId = "262261304658493440";

        try {
            activityService.attendActivtiy(activityId,userId);
            return JsonResult.success().setCode(200).setMsg("参加活动成功");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("参加活动失败");
        }
    }


}
